#!/usr/bin/env python
# -*- coding:utf-8 -*-
import contextlib
import threading
import time
import random

doing = []
def num(list2):     #启一个线程，始终在打印doing列表中的数量(正在执行的线程)
    while True:
        print len(list2)
        time.sleep(1)
t = threading.Thread(target=num, args=(doing,))
t.start()     #<Thread(Thread-1, started 2368)>

@contextlib.contextmanager
def show(l1,item):
    doing.append(item)
    yield
    doing.remove(item)

def task(i):
    flag = threading.current_thread()
    #print "flag:",flag  #flag: <Thread(Thread-2, started 6544)>......
    with show(doing,flag):
        import time
        time.sleep(random.randint(1,4))

for i in range(20):
    temp = threading.Thread(target=task,args=(i,))
    temp.start()
